<?php
class PurchasesController extends AppController {

	var $name = 'Purchases';

	var $paginate = array(
		'Purchase' => array('limit' => '20', 'order' => 'Purchase.date DESC')
	);

	function beforeFilter() {
		parent::beforeFilter();
		$this->set('menu_select', 'purchases');
	}

	function index() {
		$this->Purchase->recursive = 0;
		$this->set('purchases', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid Purchase.', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->set('purchase', $this->Purchase->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Purchase->create();

			$d = $this->data['Purchase']['date'];
			$d = explode('/', $d);
			$d = array_reverse($d);
			$d = implode('-', $d);
			$this->data['Purchase']['date'] = $d;

			if ($this->Purchase->validates()) {
				if ($this->data['Purchase']['category_id'] == 0) {
					$cat = $this->data['Purchase']['new_category_name'];

					$this->Purchase->Category->create();
					$this->Purchase->Category->save(array('Category' => array('name'=>$cat)));
					$new_cat_id = $this->Purchase->Category->getLastInsertID();
					$this->data['Purchase']['category_id'] = $new_cat_id;
				}
			}

			if ($this->Purchase->save($this->data)) {
				$this->Session->setFlash(__('The Purchase has been saved', true));
				$this->redirect(array('action'=>'qadd'));
			} else {
				$this->Session->setFlash(__('The Purchase could not be saved. Please, try again.', true));
				$this->redirect(array('action'=>'qadd'));
			}
		} else {
			$this->redirect(array('action'=>'qadd'));
		}
	}

	function qadd() {

		$ex_ids = array();

		$top = $this->Purchase->Category->find('all',
			array(
				'limit'=>'5',
				'order'=>'Category.purchase_count DESC')
			);
		$ex_ids = Set::extract($top, '{n}.Category.id');

		$recent = $this->Purchase->Category->find('all',
			array(
				'limit'=>'7',
				'order'=>'Category.modified DESC',
				'conditions' => array('NOT' => array('Category.id'=>$ex_ids))
			)
		);
		$ex_ids = array_merge($ex_ids, Set::extract($recent, '{n}.Category.id'));

		$more = $this->Purchase->Category->find('all',
			array(
				'order'=>'Category.name ASC',
				'conditions' => array('NOT' => array('Category.id'=>$ex_ids))
			)
		);
		$ex_ids = array_merge($ex_ids, Set::extract($more, '{n}.Category.id'));

		$allCategories = $this->Purchase->Category->find('list',
			array(
				'order'=>'Category.name ASC',
				'conditions' => array('NOT' => array('Category.id'=>$ex_ids))
			)
		);

		$catTypes = array(
			'top' => $top,
			'recent' => $recent,
			'more' => $more
		);

		$paymentTypes = $this->Purchase->PaymentType->find('list');

		$purchases = $this->Purchase->find('all', array('limit'=>'10', 'order'=>'Purchase.date DESC, Purchase.id DESC'));

//		$users = $this->Purchase->User->find('list');
		$this->set(compact('catTypes', 'allCategories', 'paymentTypes', 'purchases'));

		$this->set('menu_select', 'add');
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid Purchase', true));
			$this->redirect(array('action'=>'index'));
		}
		if (!empty($this->data)) {
			if ($this->Purchase->save($this->data)) {
				$this->Session->setFlash(__('The Purchase has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Purchase could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Purchase->read(null, $id);
		}
		$categories = $this->Purchase->Category->find('list', array('order'=>'Category.name ASC'));
		$paymentTypes = $this->Purchase->PaymentType->find('list');
		$users = $this->Purchase->User->find('list');
		$this->set(compact('categories','paymentTypes','users'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for Purchase', true));
			$this->redirect(array('action'=>'index'));
		}
		//We need to read the data into the model before deleting, so we can use the date field to update period totals
		$this->Purchase->recursive = -1;
		$this->Purchase->data = $this->Purchase->read(null,$id);
		if ($this->Purchase->del($id)) {
			$this->Session->setFlash(__('Purchase deleted', true));
			$this->redirect(array('action'=>'index'));
		}
	}

	function starred() {
		$this->paginate['Purchase']['conditions'] = array('Purchase.starred'=>'1');
		$this->set('purchases', $this->paginate());
	}

	function star($id) {
		$this->log('star '.$id);
		$this->Purchase->id = $id;
		$this->Purchase->saveField('starred', 1);
		$this->set('id', $id);
		$this->set('starred', 1);
		$this->render('star');
	}

	function unstar($id) {
		$this->log('unstar '.$id);
		$this->Purchase->id = $id;
		$this->Purchase->saveField('starred', 0);
		$this->set('id', $id);
		$this->set('starred', 0);
		$this->render('star');
	}

}
?>